<?php

namespace app\health\model;

use Exception;
use think\Model;
use think\model\relation\BelongsTo;

class HealthFuluOrderItem extends Model
{
    protected $insert = ['customer_order_no'];

    protected $type = [
        'status' => 'integral'
    ];

    protected $append = ['status_txt'];

    /**
     * 写入订单号
     * @return string
     * @throws Exception
     */
    public function setCustomerOrderNoAttr(): string
    {
        return create_trade_no();
    }

    /**
     * 获取充值状态描述
     * @param $val
     * @param $data
     * @return string
     */
    public function getStatusTxtAttr($val, $data): string
    {
        $map = [0 => '充值中', 1 => '充值成功', 2 => '充值失败'];
        return $map[$data['status']];
    }

    /**
     * 关联订单信息
     * @return BelongsTo
     */
    public function orderInfo(): BelongsTo
    {
        return $this->belongsTo(HealthFuluOrder::class, 'order_id');
    }
}